package state;

import java.util.Vector;

import particle.core.tools.ByteBuffer;
import particle.core.tools.PRandom;
import state.util.GameColor;
import state.util.array.ByteArray;

public class GameImage {
  private particle.core.view.assets.Image image;

  private String name = "";

  private String category = "";

  private boolean checkStatic = false;

  private Vector listeners = null;

  private int lastTimeUse = (int) System.currentTimeMillis();

  private PRandom random = new PRandom(1000);

  public GameImage(particle.core.view.assets.Image image1, String category1, String name1) {
    this.image = image1;
    if (name1 == null) {
      this.name = String.valueOf(System.currentTimeMillis()) + random.rand();
    } else {
      this.name = name1;
    }
    if (category1 != null) {
      this.category = category1;
    }
  }

  public void addUpdateImageListener(LoadResourceCompleteListener listener) {
    if (listeners == null) {
      listeners = new Vector();
    }
    listeners.addElement(listener);
  }

  public GameImage setStatic(boolean isStatic1) {
    this.checkStatic = isStatic1;
    return this;
  }

  public boolean isStatic() {
    return checkStatic;
  }

  public String getName() {
    return name;
  }

  public void setName(String name1) {
    this.name = name1;
  }

  public void setCategory(String category1) {
    this.category = category1;
  }

  public String getCategory() {
    return category;
  }

  public void setLastTimeUse(int lastTimeUse1) {
    this.lastTimeUse = lastTimeUse1;
  }

  public int getLastTimeUse() {
    return lastTimeUse;
  }

  public particle.core.view.assets.Image getImage() {
    return image;
  }

  public void setImage(particle.core.view.assets.Image image1) {
    this.image = image1;
    if (listeners != null) {
      for (int i = 0; i < listeners.size(); i++) {
        LoadResourceCompleteListener listener = (LoadResourceCompleteListener) listeners.elementAt(i);
        listener.onLoadResourceDone(this);
      }
    }
  }

  public int getWidth() {
    if (image == null || !image.isLoaded()) {
      return 0;
    }
    return image.getWidth();
  }

  public static GameImage createImage(ByteArray inputData, int offset, int size) {
    ByteArray data = inputData;
    if (size != inputData.len()) {
      data = ByteArray.create(size);
      ByteArray.arraycopy(inputData, offset, data, 0, size);
    }
    ByteBuffer byteBuffer = new ByteBuffer();
    byteBuffer.appendBytes(data.array, data.len());
    GameImage image = ResourceManager.createEmptyImage();
    particle.core.view.assets.Image coreImage = particle.core.view.assets.Image.newImage();
    coreImage.fromBuffer(byteBuffer, particle.core.view.assets.Image.FormatPNG);
    image.setImage(coreImage);
    return image;
  }

  public static GameImage createImage1(String name) {
    GameImage image = ResourceManager.createEmptyImage();
    particle.core.view.assets.Image coreImage = particle.core.view.assets.Image.newImage();
    ResourceManager resourceManager = GameGlobal.systemCanvas.getResourceManager();
    resourceManager.loadImageFromJar(coreImage, name);
    image.setImage(coreImage);
    return image;
  }

  public static GameImage createImage2(int width1, int height1) {
    GameImage image = ResourceManager.createEmptyImage();
    particle.core.view.assets.Image coreImage = particle.core.view.assets.Image.newImage();
    coreImage.create(width1, height1, true, GameColor.WHITE_CODE);
    image.setImage(coreImage);
    return image;
  }

  public static GameImage createImage4(ByteBuffer byteBuffer) {
    ByteArray data = ByteArray.create(byteBuffer.getLength());
    ByteArray.arraycopy(new ByteArray(byteBuffer.getBuffer(), byteBuffer.getLength()), 0, data, 0, byteBuffer.getLength());
    return createImage(data, 0, data.len());
  }

  public int getHeight() {
    if (image == null || !image.isLoaded()) {
      return 0;
    }
    return image.getHeight();
  }

  public static void detroyImage(GameImage image) {
    if (image != null) {
      image.setStatic(false);
      ResourceManager.removeImage(image.getCategory(), image.getName());
      image.setImage(null);
    }
  }

  public String toString() {
    return "[" + getName() + "," + getWidth() + "," + getHeight() + "]";
  }
}
